package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     * 保存套餐数据
     * @param setmealDto
     * @return
     */
    @PostMapping
    @CacheEvict(cacheNames = "setmealCache",key = "setmealDto.categoryId")
    public Result save(@RequestBody SetmealDTO setmealDto){
        setmealService.saveWithDish(setmealDto);
        return Result.success();
    }

    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
        PageResult pageResult = setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 动态删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true) // allEntries = true 这个属性指的是删除所有
    public Result delete(@RequestParam List<Long> ids){
            setmealService.deteleByIds(ids);
            return Result.success();
    }

    /**
     * 通过套餐id 查询套餐的全部信息
     * @param id
     * @return
     */

    @GetMapping("/{id}")
    public Result<SetmealVO> getById(@PathVariable long id){
         SetmealVO sv = setmealService.getSetmealVoById(id);
         return Result.success(sv);
    }

    /**
     * 修改套餐
     * @param setmealDto
     * @return
     */

    @PutMapping
    @CacheEvict(cacheNames = "setmealCache",key = "setmealDto.categoryId")
    public Result update(@RequestBody SetmealDTO setmealDto){
        setmealService.updateWithDish(setmealDto);
        return Result.success();
    }

    /**
     * 启用或停用 套餐
     * @param status
     * @param id
     * @return
     */

    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result startOrStop(@PathVariable Integer status, Long id) {
        setmealService.startOrStop(status, id);
        return Result.success();
    }


}


